import {saveCampaign, queryCampaignByUID, queryCampaignInfoByID} from '@/api/campaign'
import {queryCommentByCID} from '@/api/comment'
import {getToken, setToken, removeToken} from '@/utils/auth'

const campaign = {
    state: {
        item: {
            id: "",
            price: '',
            priceType: '',
            title: '',
            tagline: '',
            campaignImage: '',
            locationCity: '',
            locationCountry: '',
            campaignDuration: '',
            category: '',
            tagList: [],
            tag: "",

            pitchVideo: '',
            pitchImage: '',
            overlayImage: '',
            campaignOverview: '',
            campaignPitch: '',

            campaignStory: '',

            ownerId: '',
            ownerName: '',

        },
        peaksList: [],
        currentCampainId: "",
        currentCampainCategory: "",
        canDraw: false
    },

    mutations: {
        SET_PRICE: (state, price) => {
            state.item.price = price
        },
        SET_CURRENT_ID: (state, id) => {
            state.currentCampainId = id
        },
        SET_DRAW_STATUS: (state, status) => {
            state.canDraw = status
        },
        SET_CURRENT_CATEGORY: (state, category) => {
            state.currentCampainCategory = category
        },
        ADD_CAMPAIGN_PEAK: (state, item) => {
            state.peaksList.push(item)
        },
        SET_PRICE_TYPE: (state, priceType) => {
            state.item.priceType = priceType
        },
        SET_TITLE: (state, title) => {
            state.item.title = title
        },
        SET_TAGLINE: (state, tagline) => {
            state.item.tagline = tagline
        },
        SET_CAMPAIGN_IMAGE: (state, campaignImage) => {
            state.item.campaignImage = campaignImage
        },
        SET_CAMPAIGN_DESC: (state, campaignDesc) => {
            state.item.campaignDesc = campaignDesc
        },
        SET_LOCATION_CITY: (state, locationCity) => {
            state.item.locationCity = locationCity
        },
        SET_CATEGORY: (state, category) => {
            state.item.category = category
        },
        SET_LOCATION_COUNTRY: (state, locationCountry) => {
            state.item.locationCountry = locationCountry
        },
        SET_CAMPAIGN_DURATION: (state, campaignDuration) => {
            state.item.campaignDuration = campaignDuration
        },
        SET_PITCH_VIDEO: (state, pitchVideo) => {
            state.item.pitchVideo = pitchVideo
        },
        SET_PITCH_IMAGE: (state, pitchImage) => {
            state.item.pitchImage = pitchImage
        },
        SET_OVERLAY_IMAGE: (state, overlayImage) => {
            state.item.overlayImage = overlayImage
        },

        SET_OWNER_ID: (state, ownerId) => {
            state.item.ownerId = ownerId
        },
        SET_OWNER_NAME: (state, ownerName) => {
            state.item.ownerName = ownerName
        },
        SET_TAG: (state, tag) => {
            state.item.tagList = tag
        },
        SET_CAMPAIGN_OVERVIEW: (state, overviewDesc) => {
            state.item.campaignOverview = overviewDesc
        },
        SET_CAMPAIGN_PITCH: (state, pitch) => {
            state.item.campaignPitch = pitch
        },
        SET_CAMPAIGN_STORY: (state, campaignStory) => {
            state.item.campaignStory = campaignStory
        },
        EMPTY_CAMPAIGN: (state) => {
            state.item = {}
        },
        EMPTY_PEAK_LIST: (state) => {
            state.peaksList = []
        }
    },

    actions: {

        EMPTY_PEAK_LIST: ({commit}, data) => {
            commit("EMPTY_PEAK_LIST")
        },
        SET_DRAW_STATUS: ({commit}, data) => {
            commit("SET_DRAW_STATUS", data)
        },

        ADD_CAMPAIGN_PEAK: ({commit}, data) => {
            commit("ADD_CAMPAIGN_PEAK", data)
        },

        SET_CAMPAIGN_ID: ({commit}, data) => {
            commit("SET_CURRENT_ID", data)
        },
        SET_CURRENT_CATEGORY: ({commit}, data) => {
            commit("SET_CURRENT_CATEGORY", data)
        },

        SAVE_CAMPAIGN_INFO: ({commit}, data) => {
            commit(data.mutationName, data.value)
        },

        createNewPeak({commit}, peak) {
            commit("ADD_CAMPAIGN_PEAK", peak)
        },

        // 保存活动信息
        createNewCampaign({commit}, saveData) {

            // var campaignInfo = saveData

            saveData.campaignInfo.tag = saveData.campaignInfo.tagList.join(",")

            return new Promise((resolve, reject) => {
                saveCampaign(saveData).then((res) => {
                    // 填充字段
                    // commit(saveData.campaignInfo)
                    resolve(res)
                }).catch(error => {
                    reject(error)
                })
            })


        },
        // 根据UID筛选活动
        listCampaignByUID({commit}, params) {

            return new Promise((resolve, reject) => {
                queryCampaignByUID(params).then(response => {
                    // 填充字段
                    console.log(response)
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })


        },

        // 根据CID查询活动详情
        listCampaignByCID({commit}, cid) {

            return new Promise((resolve, reject) => {
                queryCampaignInfoByID(cid).then(response => {
                    // 填充字段
                    console.log(response)
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })


        },
        // 根据CID查询评论
        listCommentsByCID({commit}, cid) {

            return new Promise((resolve, reject) => {
                queryCommentByCID(cid).then(response => {
                    // 填充字段
                    console.log(response)
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })


        },

    }
}

export default campaign
